<?php
/**
 * @author Arsene Tochemey GANDOTE<arsene.gandote@yahoo.com>
 * @access public
 * @version 1.0
 * @copyright (c) 2011 - Tochemey. .
 */

//$newUserModulesArray = array();
//foreach($userData['Module'] as $userModules){
    
   // $userModules['ModulesUser']['name'] = $userModules['name'];
   // $newUserModulesArray['ModulesUser'][$userModules['ModulesUser']['id']] = $userModules['ModulesUser'];
//}
$newUserModulesArrayIds = array();
$newUserModulesArrayPriv = array();
foreach($userData['Module'] as $userModules){
    $newUserModulesArrayIds[] = $userModules['id'];
    $newUserModulesArrayPriv[$userModules['id']] = $userModules['ModulesUser']['priv'];
}

# Instance of Module Model
$moduleObj =  ClassRegistry::init('Module');
# List of all the modules in the system
$allModules = $moduleObj->find('all', array('conditions'=>array('Module.active'=>1),'recursive' => -1, 'order' => 'Module.name ASC')); 

$userId = $userData['User']['id'];
$loggedUser =  $this->Session->read('userSessionData');
$loggedUser_id = $loggedUser['id'];
$modified_date = date('Y-m-d H:i:s');
$created_date = date('Y-m-d H:i:s');
 
 //echo debug($newUserModulesArrayPriv);
?>

 <?php echo $this->Form->create('ModulesUser', array('default' => false, 'id' => 'modperm-form', 'url' => array('controller' => 'Administration', 'action' => 'changeModPerm'),'inputDefaults' => array('label' => false,'div' => false)));?>
	<div class="row-2">
     <table cellpadding="4" cellspacing="4" border="0">
             <tr>
                    <td align="left" valign="middle" nowrap>User Name</td>
                    <td><div id="username-text-div"><b><?php echo $userData['User']['username'] ;?></b></div><?php //echo $this->Form->input("User.id", array('type'=>'hidden','label' => false, 'value'=>"$userId")); ?> </td>
             </tr>
             <tr>
                    <td colspan="2" align="center" valign="middle">Select the modules and their permissions that this user can use</td>
             </tr>
             <?php
               if(!empty($allModules)){
                        $module_count = 0;
                        foreach($allModules as $module){
                            $module_id = $module['Module']['id'];
                            $checkedStatus = false;
                           
                            if(in_array($module_id,$newUserModulesArrayIds)){
                                $checkedStatus = true;
                            }
                            
                            $optionSeleted = isset($newUserModulesArrayPriv[$module_id]) ? $newUserModulesArrayPriv[$module_id] : 0;
                         ?>
                            <tr><td><?php echo $this->Form->input("ModulesUser.$module_count.module_id", array('type'=>'checkbox', 'div' => false, 'label' => false, 'class' => '', 'value'=>''.$module_id.'', 'checked'=>$checkedStatus)); ?><?php echo $module['Module']['name']; ?></td><td> <?php echo $this->Form->input("ModulesUser.$module_count.priv", array('options' => array('1'=>'READ', '2'=>'READ / WRITE', '3'=>'READ / WRITE / EXECUTE'),'selected' => ''.$optionSeleted.''));?>   <?php echo $this->Form->input("ModulesUser.$module_count.user_id", array('type'=>'hidden','label' => false, 'value'=>"$userId")); ?>   <?php echo $this->Form->input("ModulesUser.$module_count.created_by", array('type'=>'hidden','label' => false, 'value'=>"$loggedUser_id")); ?>  <?php echo $this->Form->input("ModulesUser.$module_count.creation_date", array('type'=>'hidden','label' => false, 'value'=>"$created_date")); ?></td></tr>
                         <?php
                         $module_count++;
                        }
                        
                    }
             ?>
		  </table>
	</div>
 <?php echo $this->Form->end();?>
